/* 
 * File:   DTAReader.h
 * Author: fozut
 *
 * Created on 7 de Janeiro de 2010, 19:43
 */

#ifndef _DTAREADER_H
#define	_DTAREADER_H

#include <iostream>
#include <string>
#include <fstream>

#include "DTAHash.h"
#include "DTAPonto.h"
#include "DTATriMesh.h"
#include "DTATriangulo.h"


//#include "DTAPonto.h"
using namespace std;

class DTAReader
{
public:
    DTAReader(string nameFile);
    ~DTAReader();
    DTATriMesh* readModel();
    DTAHash<DTAPonto>* readPoints();
    DTAPonto vectPt[200000];
    DTATriangulo vectTr[200000];
    int getNumberPoints();
    int getNumberTriangles();

private:
    fstream fileReader;
    string nameFile;
    int nPoints;
    int nTriangles;
    DTATriMesh *DMesh;


};

DTAReader::DTAReader(string nameFile)
{
    this->nameFile = nameFile;
    nPoints = 0;
    nTriangles = 0;
    DMesh = new DTATriMesh;
}

DTAReader::~DTAReader()
{
    
}

DTAHash<DTAPonto>* DTAReader::readPoints()
{
    DTAHash<DTAPonto> *DHash = new DTAHash<DTAPonto>;
    char str[200];
    char separator[] = " ";
    int cont =0;   
    DTAPonto pt;






    fileReader.open(nameFile.data(),fstream::in);

    fileReader.getline(str,200,separator[0]);    
    fileReader.getline(str,200,separator[0]);
    nPoints = atoi(str);
    fileReader.getline(str,200,separator[0]);
    fileReader.getline(str,200,separator[0]);
    nTriangles = atoi(str);


    cout << nPoints << " " << nTriangles << endl;


    while( fileReader.eof() == false && cont<nPoints )
    {
        fileReader.getline(str,200,separator[0]);
        pt.x = atof(str);
        fileReader.getline(str,200,separator[0]);
        pt.y = atof(str);
        fileReader.getline(str,200,separator[0]);
        pt.z = atof(str);

        pt.print();
        DHash->addData(pt);
        vectPt[cont] = pt;


        cont++;
    }

    fileReader.close();
    return DHash;

}

DTATriMesh* DTAReader::readModel()
{
    char str[200];
    char separator[] = " ";
    int cont =0;
    
    DTAPonto pt;
    DTATriangulo tr;
    int idP1;
    int idP2;
    int idP3;
    

  
    


    fileReader.open(nameFile.data(),fstream::in);

    fileReader.getline(str,200,'\n');
    cout <<"saida do aqrq: " <<str << endl;
    fileReader.getline(str,200,'\n');
    nPoints = atoi(str);
    fileReader.getline(str,200,'\n');
    fileReader.getline(str,200,'\n');
    nTriangles = atoi(str);

    cout << "Na leitura em DTAReder: " <<nPoints << " " << nTriangles << endl;
   
    //le os pontos e os coloca num vetor de pontos
    while( fileReader.eof() == false && cont<nPoints )
    {
        fileReader.getline(str,200,separator[0]);
        pt.x = atof(str);
        fileReader.getline(str,200,separator[0]);
        pt.y = atof(str);
        fileReader.getline(str,200,'\n');
        pt.z = atof(str);
        pt.id =cont;

        //pt.print();
        vectPt[cont] = pt;
       
        cont++;
    }

    //le os triangulos

    cont =0;
    while(fileReader.eof() == false && cont < nTriangles)
    {
        fileReader.getline(str,200,separator[0]);
        idP1 = atoi(str);
        fileReader.getline(str,200,separator[0]);
        idP2 = atoi(str);
        fileReader.getline(str,200,'\n');
        idP3 = atoi(str);

        tr.p1 = vectPt[idP1];
        tr.p2 = vectPt[idP2];
        tr.p3 = vectPt[idP3];
        

        vectTr[cont] = tr;
        DMesh->addCell(tr);
       // cout << idP1 << " " << idP2 << " " << idP3 << endl;
        cont++;
    }

   // DMesh->lalala();
   
    fileReader.close();

    return DMesh;

}

int DTAReader::getNumberPoints()
{
    return nPoints;
}

int DTAReader::getNumberTriangles()
{
    return nTriangles;
}

#endif	/* _DTAREADER_H */

